package Navi_run;

import encryption.Hash;
import it.unisa.dia.gas.jpbc.Element;
import objects.LocationBean;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Navigation_respond {
    // rsu或者nsp响应司机的导航请求
    public static boolean verifyRequest() {
        //计算Ei = e(REi1, A)^ci · e(REi2, g))^ci · e(REi1, B)^ssi
        Element Ei = (params.pairing.pairing(Rsu_driver.REi1, params.gpk_A).pow(Rsu_driver.ci)).mul(
                (params.pairing.pairing(Rsu_driver.REi2, params.gpk_g).pow(Rsu_driver.ci)).negate()
        ).mul(params.pairing.pairing(Rsu_driver.REi1, params.gpk_B).pow(Rsu_driver.ssi));
        // 计算cii 与司机发来的ci比较
        String temp = Rsu_driver.REi1.toString() + Rsu_driver.REi2.toString() + Ei.toString() + Rsu_driver.Mi + Rsu_driver.rci;
        BigInteger cii = new BigInteger(Hash.sha256(temp));
        if (cii.compareTo(Rsu_driver.ci) == 0) {
            System.out.println("验证rdi导航查询或cdi提交报告请求成功");
            return true;
        } else {
            System.out.println("验证rdi导航查询或cdi提交报告请求失败");
            return false;
        }
    }

    //将<rlpi,NQi>插入到Tru数据库中
    public static void insertIntoTru(BigInteger rlpi, Map<String, Object> NQ) {

    }

    //RSU广播交通拥堵任务
    public static void broadcastTask() {

    }

    //cdi发起邻近挑战
    public static void broadchallenge() {

    }

    //cdi将报告发送给RSU
    public static void sendReported(List<LocationBean> locationBeans) {
        //时间历元te
        String te = "123465";
        //生成NQ
        Map<String, Object> NQ;
        NQ = Navigation_query.driver_generate_NQ(te, TA_driver.rci, TA_driver.si, Navigation_query.driver_generate_query(locationBeans, false),false);
        Map<String, Object> DR = new HashMap<>();
        DR.put("Mj",NQ.get("Mi"));
        DR.put("Query",NQ.get("Query"));
        DR.put("REi1",NQ.get("REi1"));
        DR.put("REi2",NQ.get("REi2"));
        DR.put("ci",NQ.get("ci"));
        DR.put("ssi",NQ.get("ssi"));
        Rsu_driver.DR= DR;
    }

    public static void gnerateDatabases(){

    }

    public static void main(String[] args) {
        try {
            Navigation_query.transition();
            verifyRequest();
            DriverTrack.sendSignToTA();
            DriverTrack.TAresponse();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
